package wevil.spring.conf;

import org.springframework.orm.hibernate4.HibernateTransactionManager;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.SessionFactoryUtils;

/**
 * 事务管理，实现动态获取数据源和sessionFactory
 * Created by Administrator on 2017/11/2.
 */
public class DynamicTransactionManager extends HibernateTransactionManager {
    @Override
    public DataSource getDataSource() {
        return SessionFactoryUtils.getDataSource(getSessionFactory());
    }

    @Override
    public SessionFactory getSessionFactory() {
        DynamicSessionFactory dynamicSessionFactory = (DynamicSessionFactory) super.getSessionFactory();
        return dynamicSessionFactory.getHibernateSessionFactory();
    }
}
